package com.kfm.jdbc.day0404.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket2 implements Runnable{

    // 定义100张票
    private int ticket = 100;

    // 定义一个锁对象
    ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        // 模拟窗口一直在卖票
        while (true) {
            lock.lock();
            try{
                sellTicket();
            } finally {
                lock.unlock();
            }
        }
    }

    public void sellTicket(){

        if (ticket > 0) {
            // 模拟卖票时间
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 模拟卖票操作
            System.out.println(Thread.currentThread().getName() + " 正在卖第 " + ticket-- + "张票");
        }

    }
}
